package com.athuawei.callable;


import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

// 比较两个接口到区别
// 实现Runnable接口
class MyThread1 implements Runnable {

    @Override
    public void run() {

    }
}

// 实现Callable接口
class MyThread2 implements Callable{

    @Override
    public Integer call() throws Exception {

        System.out.println(Thread.currentThread().getName() + " come in callable");
        return 200;
    }
}

public class Demo1 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // Runnable接口创建线程
        new Thread(new MyThread1(), "AA").start();

        // Callable接口创建线程
        // 到java中找一个类，既和Runnable有关系，又和Callable有关系
        // Runnable接口又实现类FutureTask
        // FutureTask构造可以传递Callable

        // FutureTask
        FutureTask<Integer> futureTask1 = new FutureTask<>(new MyThread2());
        // lamda表达式
        FutureTask<Integer> futureTask2 = new FutureTask<>(()->{
            System.out.println(Thread.currentThread().getName() + " come in callable");
            return 1024;
        });

        // 创建一个线程
        new Thread(futureTask2, "BB").start();
        new Thread(futureTask1, "AA").start();

//        while(!futureTask2.isDone()){
//            System.out.println("wait......");
//        }

        // 调用FutureTask的get方法
        System.out.println(futureTask2.get());
        System.out.println(futureTask1.get());

        System.out.println(Thread.currentThread().getName() + " game over!");
        // FutureTask原理，未来任务
        /**
         * 1、
         */


    }
}
